﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using System.Collections.ObjectModel;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using ASoft.Repository;
using System.Text.RegularExpressions;
using ASoft.Base;

namespace ASoft.WPF.ViewModel
{
    public abstract class DBSearchViewModel<T> : ViewModelBase where T : class,new()
    {
        ObservableCollection<T> _数据集合;
        public ObservableCollection<T> 数据集合
        {
            get
            {
                if (_数据集合 == null)
                {
                    _数据集合 = new ObservableCollection<T>();
                }
                return _数据集合;
            }
        }

        protected void Reload数据集合()
        {
            if (StartDate > EndDate)
            {
                EndDate = StartDate;
            }
            if (StartDate.AddYears(1) < EndDate)
            {
                EndDate = StartDate.AddYears(1);
            }
            数据集合.Clear();
            foreach (var item in GetSearchResult())
            {
                数据集合.Add(item);
            }
        }

        ObservableCollection<SeriesBindObject> _Series数据集合;
        public ObservableCollection<SeriesBindObject> Series数据集合
        {
            get
            {
                if (_Series数据集合 == null)
                {
                    _Series数据集合 = new ObservableCollection<SeriesBindObject>();
                }
                return _Series数据集合;
            }
        }

        protected void ReloadSeries数据集合()
        {
            Series数据集合.Clear();

            var firstSelectProperty = this.GetType().GetProperties()
                .Where(p => p.Name.StartsWith("IsSeries"))
                .Where(p => (bool)p.GetValue(this, null))
                .FirstOrDefault();

            if (firstSelectProperty == null)
            {
                return;
            }

            foreach (var item in GetSeriesBindObjects(firstSelectProperty.Name.Replace("IsSeries", "")))
            {
                Series数据集合.Add(item);
            }
        }

        protected virtual IEnumerable<SeriesBindObject> GetSeriesBindObjects(string propertyName)
        {
            return new List<SeriesBindObject>();
        }

        public ICommand SearchCommand
        {
            get
            {
                return new RelayCommand(
                    () =>
                    {
                        Reload数据集合();
                        ReloadSeries数据集合();
                    },
                    () =>
                    {
                        if (IsInDesignMode)
                        {
                            return false;
                        }
                        return true;
                    });
            }
        }

        #region "StartDate"

        private DateTime _StartDate = DateTime.Now.Date;

        public DateTime StartDate
        {
            get
            {
                return _StartDate;
            }
            set
            {
                if (_StartDate == value)
                {
                    return;
                }

                _StartDate = value;
                RaisePropertyChanged("StartDate");
            }
        }

        #endregion

        #region "EndDate"

        private DateTime _EndDate = DateTime.Now.Date;

        public DateTime EndDate
        {
            get
            {
                return _EndDate;
            }
            set
            {
                if (_EndDate == value)
                {
                    return;
                }

                _EndDate = value;
                RaisePropertyChanged("EndDate");
            }
        }

        #endregion


        protected IEnumerable<T> GetSearchResult()
        {
            List<T> items = new List<T>();
            int year = StartDate.Year;
            while (year <= EndDate.Year)
            {
                Repository.DBFileName = year.ToString();
                items.AddRange(Repository.GetEntities(GetCondition()));
                year++;
            }
            return OrderSearchResult(items);
        }

        protected abstract IEnumerable<T> OrderSearchResult(IEnumerable<T> items);

        private Dictionary<string, string> GetCondition()
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            typeof(T).GetProperties().ToList().ForEach(p =>
                {
                    var p1 = this.GetType().GetProperty(p.Name);
                    if (p1 == null)
                    {
                        return;
                    }
                    var value = p1.GetValue(this, null);
                    if (value == null)
                    {
                        return;
                    }
                    if (string.IsNullOrWhiteSpace(value.ToString()))
                    {
                        return;
                    }
                    result[p.Name] = Regex.Replace(value.ToString(), @"[^(,|:|\d)]", "");
                });
            result["时间"] = string.Format("{0}:{1}", StartDate.ToShortDateString(), EndDate.AddDays(1).ToShortDateString());
            return result;
        }

        protected abstract IRepository<T> Repository { get; }
    }
}
